package com.lehezn.opensource.appkit.ControlModule;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by ll on 2017/8/24.
 */

public class Util {
    public static byte ACTION_ADD = 1;
    public static byte ACTION_DELETE = 2;
    public static byte ACTION_MODIFY = 3;
    public static byte ACTION_NOT_CHANGE = 4;

    public static final byte TYPE_SINGLE_METER = 0;
    public static final byte TYPE_MUTIL_METER = 1;
    public static final byte TYPE_WATER = 0x10;
    public static final byte TYPE_HEAT = 0x20;
    public static final byte TYPE_GOS = 0x30;

    //0：正常下发操作表数据；1：读取实时数据，2：读取当前数据；3：读取日冻结；4：读取月冻结
    public  static  enum readType{
        readRealData,readCuurentData,readDayFreeze,readMonthFreeze

    }

    public static final String PRICE_JIAN="e_jian";
    public static final String PRICE_FENG="e_feng";
    public static final String PRICE_PING="e_ping";
    public static final String PRICE_GU="e_gu";
    public static final String PRICE_WATER="water";
    public static final String PRICE_GOS="gos";
    public static final String PRICE_HEART="heart";
//    public static final String PRICE_;

    /**
     * <item>单项电能表</item>0
     * <item>三项电能表 </item>1
     * <item>水表</item>0x10
     * <item>热表</item>0x20
     * <item>气表</item>0x30
     */
    public static byte convertSpinnerToByte(byte itemPosition) {
        switch (itemPosition) {
            case 0:
                return TYPE_SINGLE_METER;
            case 1:
                return TYPE_MUTIL_METER;
            case 2:
                return TYPE_WATER;
            case 3:
                return TYPE_HEAT;
            case 4:
                return TYPE_GOS;
        }
        return TYPE_SINGLE_METER;
    }

    public static byte convertTypeToSpinnerPos(byte itemPosition) {
        switch (itemPosition) {
            case TYPE_SINGLE_METER:
                return 0;
            case TYPE_MUTIL_METER:
                return 1;
            case TYPE_WATER:
                return 2;
            case TYPE_HEAT:
                return 3;
            case TYPE_GOS:
                return 4;
        }
        return 0;
    }
//    public static byte convertBuardToSpinnerPos(byte buard) {
//        switch (buard) {
//            case TYPE_SINGLE_METER:
//                return 0;
//            case TYPE_MUTIL_METER:
//                return 1;
//            case TYPE_WATER:
//                return 2;
//            case TYPE_HEAT:
//                return 3;
//            case TYPE_GOS:
//                return 4;
//        }
//        return 0;
//    }


    public static String bcd2Str(byte[] bytes) {
//        MyLog.d("bcd2Str_param" + new String(bytes));
        StringBuffer temp = new StringBuffer(bytes.length * 2);

        for (int i = 0; i < bytes.length; i++) {
            temp.append((byte) ((bytes[i] & 0xf0) >>> 4));
            temp.append((byte) (bytes[i] & 0x0f));
        }
//        Log.d("decode--number--------------->", temp.toString());
        int i = Integer.parseInt(String.valueOf(temp));
        String result = String.valueOf(i);
//        MyLog.d("bcd2Str_result" + result);
        return result;
//        return temp.toString().substring(0, 1).equalsIgnoreCase("0") ? temp.toString().substring(1) : temp.toString();
    }


    /**
     * 电压
     * XXX.X
     *
     * @param arr
     * @return
     */
    public static double getVol(byte[] arr) {
        arr = revertOrder(arr);

        return Double.parseDouble(Util.bcd2Str(arr)) / 10;

//        return bcd2Str(arr);
//        byte temp1 = arr[0];
//        byte temp2 = arr[1];
//        double result = temp1 * 100 + temp2;
//        return result / 10;
    }

    /**
     * 电流
     * XXX.XXX
     *
     * @param arr
     * @return
     */
    public static double getCur(byte[] arr) {
        arr = revertOrder(arr);

        return Double.parseDouble(Util.bcd2Str(arr)) / 1000;

//        byte temp1 = arr[0];
//        byte temp2 = arr[1];
//        byte temp3 = arr[2];
//        double result = temp1 * 10000 + temp2 * 100 + temp3;
//        return result / 1000;
    }

    /**
     * 功率
     * XX.XXXX
     *
     * @param arr
     * @return
     */
    public static double getPower(byte[] arr) {
        arr = revertOrder(arr);

        return Double.parseDouble(Util.bcd2Str(arr)) / 10000;

//        byte temp1 = arr[0];
//        byte temp2 = arr[1];
//        byte temp3 = arr[2];
//        double result = temp1 * 10000 + temp2 * 100 + temp3;
//        return result / 10000;

    }

    /**
     * 功率因数
     * X.XXX
     *
     * @param arr
     * @return
     */
    public static double getPowerFactory(byte[] arr) {
        arr = revertOrder(arr);

        return Double.parseDouble(Util.bcd2Str(arr)) / 1000;
//
//        byte temp1 = arr[0];
//        byte temp2 = arr[1];
//        double result = temp1 * 100 + temp2;
//        return result / 1000;
    }

//    public static double getEle(byte[] ele) {
//        byte[] decimalByte = new byte[1];
//        decimalByte[0] = ele[0];
//        String decimal = Util.bcd2Str(decimalByte);
//
//        byte[] numberByte = new byte[3];
//        numberByte[0] = ele[3];
//        numberByte[1] = ele[2];
//        numberByte[2] = ele[1];
//        String interger = Util.bcd2Str(numberByte);
//
//        String dec = decimal.substring(0, 1);
//
//        String number = interger + (decimal.equals("0") ? "" : "." + dec);
//        System.out.print("=====》电量信息decodenumber" + number);
//
//        return Double.parseDouble(number);
//    }


    /**
     * 电量
     *
     */
    public static double getEleEnery(byte[] arr) {
        arr = revertOrder(arr);

        return Double.parseDouble(Util.bcd2Str(arr)) / 100;
    }

    /**
     * 水示数
     * XX.XX
     */
    public static double getWaterEnery(byte[] arr) {
        arr = revertOrder(arr);
        return Double.parseDouble(Util.bcd2Str(arr)) / 100;
    }

    /**
     * 热示数
     * XX.XX
     */
    public static double getHeatEnery(byte[] arr) {
        arr = revertOrder(arr);
        return Double.parseDouble(Util.bcd2Str(arr)) / 100;
    }


    public static byte[] hexStringToByte(String hex) {
        int len = (hex.length() / 2);
        byte[] result = new byte[len];
        char[] achar = hex.toCharArray();
        for (int i = 0; i < len; i++) {
            int pos = i * 2;
            result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
        }
        return result;
    }

    private static byte toByte(char c) {
        byte b = (byte) "0123456789ABCDEF".indexOf(c);
        return b;
    }

    public static String bytesToHexString(byte[] array) {
        String result = "";
        for (int i = 0; i < array.length; i++) {
            result += String.format("%02X ", array[i]);
        }

        return result;
    }

    /**
     * 把byte转为字符串的bit
     */
    public static String byteToBitStr(byte b) {
        return ""
                + (byte) ((b >> 7) & 0x1) + (byte) ((b >> 6) & 0x1)
                + (byte) ((b >> 5) & 0x1) + (byte) ((b >> 4) & 0x1)
                + (byte) ((b >> 3) & 0x1) + (byte) ((b >> 2) & 0x1)
                + (byte) ((b >> 1) & 0x1) + (byte) ((b >> 0) & 0x1);
    }

    public static String[] getDayBefore(int dayCount) {

        String [] dayArr=new String[dayCount];
        for (int i=0;i<dayCount;i++){
            Calendar c = Calendar.getInstance();

            c.setTime(new Date());
            c.add(Calendar.DATE, i-(dayCount-1));

            String dayBefore = new SimpleDateFormat("MM-dd").format(c.getTime());
            dayArr[i]=dayBefore;
//            MyLog.d("getDayBefore日期-->" + i+"res=="+dayBefore);
        }
        return dayArr;
    }

    public static String[] getMonthBefore() {
        String [] monthArr=new String[12];
        for (int i=0;i<12;i++) {
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            c.add(Calendar.MONTH, i-11);

            String dayBefore = new SimpleDateFormat("yy-MM").format(c.getTime());
            monthArr[i]=dayBefore;
        }
        return monthArr;
    }


    /**
     * 把byte转化成2进制字符串
     *
     * @param b
     * @return
     */
    public static String getBinaryStrFromByte(byte b) {
        String result = "";
        byte a = b;
        ;
        for (int i = 0; i < 8; i++) {
            result = (a % 2) + result;
            a = (byte) (a / 2);
        }
        return result;
    }

    public static byte[] revertOrder(byte[] source) {
        byte[] result = new byte[source.length];
        int j = 0;
        for (int i = source.length - 1; i >= 0; i--) {
            result[j] = source[i];
            j++;
        }
        return result;
    }


}
